package com.michael;

import java.io.*;
import java.util.List;

/**
 * Created by Administrator on 2019/3/12 0012.
 */
public class ShallowClone implements Serializable{
    private String name;
    private int age;
    private List<Course> courses;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public List<Course> getCourses() {
        return courses;
    }

    public void setCourses(List<Course> courses) {
        this.courses = courses;
    }

    public ShallowClone clone(){
        ShallowClone shallowClone = new ShallowClone();
        shallowClone.setName(this.getName());
        shallowClone.setAge(this.getAge());
        shallowClone.setCourses(this.getCourses());
        return shallowClone;
    }

    public ShallowClone deepClone(){
        ShallowClone clone = null;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(this);

            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bis);

            clone = (ShallowClone) ois.readObject();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return clone;
    }
}
